/*
JuffEd - A simple text editor
Copyright 2007 Murzin Mikhail

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "HL.h"

//	Local
#include "HLStorage.h"

HL::HL(QTextDocument* doc) : QSyntaxHighlighter(doc) {
}

HL::~HL() {
}

void HL::changeFileName(const QString& name) {
	if (!HLStorage::instance()->getScheme(name, sch_)) {
		sch_ = HLScheme();
	}
	rehighlight();
}

void HL::highlightBlock(const QString& str) {
	QString text = str;
	//	comment
	int cInd = text.indexOf(QRegExp(sch_.comment));
	if (cInd >= 0) {
		setFormat(cInd, text.length() - cInd, sch_.commentFmt);
		text = text.left(cInd);
	}

	//	rules
	foreach (HLRule rule, sch_.rules_) {
		if (!rule.regExp.isValid() || rule.regExp.isEmpty())
			continue;
		QRegExp expression(rule.regExp);
		int index = text.indexOf(expression);
		while (index >= 0) {
			int length = expression.matchedLength();
			setFormat(index, length, rule.fmt);
			index = text.indexOf(expression, index + length);
		}
	}
	setCurrentBlockState(0);
}
